<?php
/*
Auth:Sang
Desc:后台用户权限组模型
Date:2014-10-27
*/
namespace Mod;
use Lib\Model;
class UserGroup extends Model{
	public function getGroupById($group_id) {
		if (! $group_id || ! is_numeric ( $group_id )) {
			return false;
		}
		$list = $this->where("group_id={$group_id}")->fetch();
		if ($list) {
			return $list;
		}
		return array ();
	}

	//列表 
	public function getAllGroup() {
		$sql="select group_id, group_name, group_role, owner_id , group_desc ,u.user_name as owner_name from ".$this->getTableName()." g left join ".model('#User','adm_')->getTableName()." u on g.owner_id =  u.user_id order by g.group_id";
		return $this->db->query($sql)->fetchAll();
	}
	
	public function addGroup($group_data) {
		if (! $group_data || ! is_array ( $group_data )) {
			return false;
		}
		return $this->insert($group_data);
	}
	
	public function getGroupByName($group_name) {
		if ( $group_name == "" ) {
			return false;
		}
		return $this->where("group_name='{$group_name}'")->fetch();
	}
	
	public function updateGroupInfo($group_id,$group_data) {
		if (! $group_data || ! is_array ( $group_data )) {
			return false;
		}
		return $this->where("group_id={$group_id}")->update($group_data);
	}
	
	public function delGroup($group_id) {
		if (! $group_id || ! is_numeric ( $group_id )) {
			return false;
		}
		return $this->where("group_id={$group_id}")->delete();
	}
	
	public function getGroupForOptions() {
		$group_list = $this->getAllGroup ();
		foreach ( $group_list as $group ) {
			$group_options_array [$group ['group_id']] = $group ['group_name'];
		}
		return $group_options_array;
	}
	
	public function getGroupUsers($group_id) {
		$sql="select  group_id, group_name, group_role, owner_id , group_desc ,u.user_id as user_id,u.user_name as user_name,u.real_name as real_name from ".$this->getTableName()." g,".model('#User','adm_')->getTableName()." u where g.group_id = $group_id and g.group_id = u.user_group order by g.group_id,u.user_id";
		return $this->db->query ($sql)->fetchAll();
	}
}